{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "%pylab inline\n",
    "import struct"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Read in data from binary waterfall file"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "header_len = 8+26 # 2 unsigned int for center_freq and span: 8 bytes PLUS 26 bytes for datetime\n",
    "nbin = 1024\n",
    "\n",
    "with open(\"wf.bin\", \"rb\") as fd:\n",
    "    buff = fd.read()\n",
    "\n",
    "length = len(buff[header_len:])\n",
    "n_t = length / nbin\n",
    "\n",
    "header = struct.unpack('2I26s', buff[:header_len])\n",
    "data = struct.unpack('%dB'%length, buff[header_len:])\n",
    "\n",
    "waterfall_array = np.reshape(np.array(data[:]), (n_t, nbin))\n",
    "waterfall_array -= 255\n",
    "\n",
    "avg_wf = np.mean(waterfall_array[:,:], axis=0)\n",
    "\n",
    "center_frequency, span, rec_time = header\n",
    "start_freq = center_frequency - span/2\n",
    "stop_freq = center_frequency + span/2\n",
    "print \"Recording date:\", rec_time"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Plot waterfall data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "figure(figsize=(40,8))\n",
    "nticks = 11\n",
    "xticks(np.linspace(0,nbin,nticks), np.linspace(start_freq, stop_freq, nticks))\n",
    "pcolormesh(waterfall_array[:,1:], cmap=cm.jet , vmin=-100, vmax=-46)\n",
    "title(\"Recording time: %s\"%rec_time)\n",
    "colorbar()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Plot a few spectra"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "figure(figsize=(20,8))\n",
    "nticks = 11\n",
    "xticks(np.linspace(0,nbin,nticks), np.linspace(start_freq, stop_freq, nticks))\n",
    "for spectrum in waterfall_array[1:10]:\n",
    "    plot(spectrum[1:])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Plot signal levels distribution (instantaneous and average)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "figure(figsize=(20,8))\n",
    "_=hist(waterfall_array[0,:], bins=40, normed=True)\n",
    "xlim(-110,0)\n",
    "xlabel(\"Signal level (dB)\")\n",
    "ylabel(\"Occurrences\")\n",
    "title(\"Instantaneous power level distribution\")\n",
    "\n",
    "figure(figsize=(20,8))\n",
    "_=hist(avg_wf, bins=40, normed=True)\n",
    "xlim(-110,0)\n",
    "xlabel(\"Signal level (dB)\")\n",
    "ylabel(\"Occurrences\")\n",
    "title(\"Average power level distribution\")\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Compute SNR for the whole band (average data)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "median = np.median(avg_wf)\n",
    "perc95 = np.percentile(avg_wf, 95)\n",
    "\n",
    "print \"SNR estimation: median: %f dB, 95th perc.: %f dB, SNR: %f dB\" % (median, perc95, perc95-median)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.15"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
